package com.museum.demo.Entity.Impl;

import com.museum.demo.Dao.MuseumEntityMapper;
import com.museum.demo.Entity.MuseumEntity;
import com.museum.demo.Entity.SubscribeEntity;
import com.museum.demo.Service.MuseumService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MuseumEntityImpl implements MuseumService {
    private static final Logger logger = LoggerFactory.getLogger(MuseumEntityImpl.class);
    @Autowired
    private MuseumEntityMapper museumEntityMapper;

    @Override
    public List<MuseumEntity> GetMuseumList() {
        List<MuseumEntity> result = new ArrayList<>();
        try{
            result = museumEntityMapper.GetMuseumListAll();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<MuseumEntity> SearchMuseum(String MuseumName) {


        List<MuseumEntity> result = new ArrayList<>();
        if(MuseumName != null) {
            result = museumEntityMapper.GetMuseumByName(MuseumName);
        }
        return result;
    }

    @Override
    public List<MuseumEntity> SearchMuseumListByLocal(String Location) {
        List<MuseumEntity> result;

        if(Location != null){
            result = museumEntityMapper.GetMuseumByPosition(Location);
        }else{
            return null;
        }

        return result;
    }

    /*
    * function:获取全部博物馆的名称
    * author: ma shan lin
    * date: 2021-2-19
    * */
    @Override
    public List<String> GetMuseumNameAll() {
        List<String> result = new ArrayList<>();
        try{
            result = museumEntityMapper.GetMuseumNameAll();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /*
    * function:获取推荐博物馆列表
    * author：ma shan lin
    * date: 2021-2-19
    * */
    @Override
    public List<MuseumEntity> GetRecommendList() {
        List<MuseumEntity> result = new ArrayList<>();
        for(int i=0;i<3;i++){
            int num = (int)(Math.random()*35+1);
            if(num>40){
                num = (int)(Math.random()*10+1);
            }
            logger.info(String.valueOf(num));
            result.add(museumEntityMapper.selectByPrimaryKey(num));
        }
        if(result.toString() != "[]"){
            return result;
        }
        return null;
    }

    /*
    * function:获取博物馆名称（非实体）根据位置
    * author: ma shan lin
    * date: 2021-2-21
    * */
    @Override
    public List<Map<String,String>> GetMuseumNameByLocal(String Postion) {
        List<String> res = new ArrayList<>();
        List<Map<String,String>> result = new ArrayList<>();

        try{
            res = museumEntityMapper.GetMseumNameByPosition(Postion);
            for(int i = 0;i<res.size();i++){
                Map<String,String> map = new HashMap<>();
                map.put("museumName",res.get(i));
                result.add(map);
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /*
    * function: 获取所有区域的名称列表
    * author: ma shan lin
    * date: 2021-2-22
    * */
    @Override
    public List<String> GetAllPosition() {
        List<String> result = new ArrayList<>();
        try{
            result = museumEntityMapper.GetAllPosition();
            return result;
        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int SubscribeMuseum(int museumid, int userid) {
        int result;
        SubscribeEntity subscribe = new SubscribeEntity();
        subscribe.setUserid(Integer.valueOf(userid));
        subscribe.setMuseumid(Integer.valueOf(museumid));
        subscribe.setStatus(Integer.valueOf(1));

        result = museumEntityMapper.SubscribeMuseum(subscribe);
        if(result != 0){
            return result;
        }
        return 0;

    }

    @Override
    public int ChangeSubscribeStatus(int museumid, int userid) {
        SubscribeEntity sub = new SubscribeEntity();
        sub.setUserid(userid);
        sub.setMuseumid(museumid);

        int result = museumEntityMapper.ChangeSubscribeStatus(sub);
        if(result != 0){
            return result;
        }
        return 0;
    }


}
